<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require APPPATH."libraries/MX/Controller.php";

class CH_Controller extends MX_Controller {

	var $_theme;
	var $_view;	
	public function init(){
		$this->_theme = '';
		$this->_view = '';
		$this->load->helper(array('text','path','form','array', 'url','common','login','ckeditor','html'));
		$this->config->load('/ch/config.php');
		//$this->load->library('simplexml');
		$this->load->library('Xmlparser');
		
		//$this->seg_exp = $this->common->segment_explode($this->uri->uri_string());
		//echo $this->uri->uri_string();
	}

	/**
	 * 최종 HTML 화면 표시
	 *
	 * @access	private
	 * @param	string
	 * @param	Array
	 * @return	string
	 */
	/*function parse($data){		
		//$this->init();
		$data = array_merge($data,array('_theme'=>$this->config->item('ch_theme')));
		$this->load->view('masterpage',$data);
		
	}
	
	function _parse($data){
		//$this->init();
		$this->load->view($data);
	}*/
	
	/**
	 * web log 수집
	 *
	 * @access	
	 * @param	
	 * @param	
	 * @return	void
	 */
	private function weblog(){
		//$this->load->library('bbclone');
		define("_BBC_PAGE_NAME", "CH");
		define("_BBCLONE_DIR", "./asset/bbclone/");
		define("COUNTER", _BBCLONE_DIR."mark_page.php");
		if (is_readable(COUNTER)) include_once(COUNTER);
		
	}
	
	function parse($data){		
		$skin_frame = ($data['_front'] == 'N') ? 'chmanager' : 'masterpage';
		$this->load->view($skin_frame,$data);
	}
	
	public function _remap($method, $params = array()){
		
		//@method assign
		//if(is_array($this->$method())) $data += $this->$method();
		$data = $this->$method();
		//@result assign
	
		if(!array_key_exists('_plugin',$data)){ //플러그인
			$data = array_merge($data,array('_plugin'=>'N'));
		}
		
		if(!array_key_exists('_theme',$data)){ //테마 추가
			$data = array_merge($data,array('_theme'=>$this->config->item('ch_theme')));
		}
		
		if(!array_key_exists('_use_theme',$data)){ //테마 사용여부
			$data = array_merge($data,array('_use_theme'=>'Y'));
		}
		
		if(!array_key_exists('_front',$data)){ //front  여부
			$data = array_merge($data,array('_front'=>'N'));
		}

		if(strpos($this->uri->uri_string(),'ajax_') > 0){
			$ajax_mathod = $this->uri->segment($this->uri->total_segments());
			//실제 method와 uri상 method 동일한경우 실제 uri 대입
			$ajax = (!strcmp($method,$ajax_mathod)) ? $data['_content'] : $this->uri->uri_string();  		
			$this->load->view($ajax,$data);
		}else if($data['_use_theme'] =='N'){
			$this->load->view($data['_content'],$data);
		}else if(strpos($this->uri->uri_string(),'index') > 0){ //인덱스 제외
			$this->weblog(); //web log
			$this->load->view($this->uri->uri_string(),$data);
			//$this->output->enable_profiler(TRUE);
		}else{
			$this->weblog(); //web log
			$this->parse($data);
			//$this->output->enable_profiler(TRUE);
		}
		
		
	}
	
	function uri_church($seq = null){
		$url = $this->uri->uri_string();
		if($seq){ //주소배열 지정
			$url='';
			for($i=1; $i<=$seq; $i++){
				$url .= ($url) ? '/'.$this->uri->segment($i) : $this->uri->segment($i);
			}	
		}else{
			if($this->uri->total_segments() >= 3){
				$url = $this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$this->uri->segment(3);
			}
		}
		$url = str_replace('html/','',$url);
		return $url;
	}
	
}
?>
